package cn.stazxr.config;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author talentestors
 * @version 1.0
 * @since 2025.11.03
 **/
@Configuration
public class ThreadPoolConfig {

	@Bean
	public ExecutorService executorService() {
		return new ThreadPoolExecutor(
			10,
			20,
			60L, TimeUnit.SECONDS,
			new ArrayBlockingQueue<>(64),
			BasicThreadFactory.builder().namingPattern("ydlclass-log-%d").daemon(true).build(),
			new ThreadPoolExecutor.AbortPolicy()
		);
	}

	@Bean("virtualExecutor")
	public ExecutorService virtualExecutor() {
		// 每个任务一个虚拟线程，无需配置 core/max/queue
		return Executors.newVirtualThreadPerTaskExecutor();
	}
}
